package com.kehutong.material.admin;

import java.util.List;
import org.coraframework.authz.HasPermission;
import org.coraframework.bean.valid.ValidResult;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.Objects;
import com.kehutong.common.entity.Root;
import com.kehutong.material.entity.QuestionPlatform;

/**
 * 问题控制器
 * @author: liukunlong
 * @date: 2021-02-19 15:12
 */
@MvcAction
@ReqMapping("/admin/crm/question/platform")
public class QuestionPlatformController {
	
	@Inject
	private JdbcSession jdbcSession;

	@HasPermission("crm:question:view")
	@ReqMapping("/tree")
	public Object listParent(JSONObject jsonObject) {
		final List<QuestionPlatform> questionList = jdbcSession.findArray(QuestionPlatform.class)
															   .eq("pid", Root.ROOT_ID)
															   .eq("isFolder", true)
															   .eq("deleted", false)
															   .exe();
		QuestionPlatform root = Pool.newInstance(QuestionPlatform.class, Root.ROOT_ID);
		root.setName("问题库管理");
		questionList.forEach(question->{
			root.addChild(question);
		});

		return Result.success(root);
	}

	@HasPermission("crm:question:view")
    @ReqMapping("/list")
    public Object list(JSONObject jsonObject) throws Exception {
		String pid = jsonObject.getString("pid");
		if (Root.isRoot(pid)) {
			jsonObject.remove("pid");
		}
		return jdbcSession.findPage(QuestionPlatform.class)
	    			.eq(jsonObject, "id", "pid")
	        		.like(jsonObject, "question", "name")
					.eq("isFolder", false)
					.eq("deleted", false)
					.order("createTime", "desc")
					.page(jsonObject)
					.exe();
    }

	@HasPermission("crm:question:view")
    @ReqMapping("/get")
    public Object get(String id) {
		QuestionPlatform question = Pool.get(QuestionPlatform.class, id);
		return Result.success(question);
    }


	@Bean(newInstance=true, copy=true)
	@HasPermission("crm:question:edit")
    @ReqMapping("/save")
    public Object save(QuestionPlatform question, ValidResult result) {
    	if (result.hasError("answer","isFolder")) {
    		return Result.error(0, result.getErrorMessage());
    	}

		List<QuestionPlatform> exe = jdbcSession.findArray(QuestionPlatform.class)
				//.eq("companyNo", tag.getCompanyNo())
				//.eq("pcode", tag.getPcode())
				// todo 需要调试
				.eq("pid", question.getPid())
				.eq("name", question.getName())
				.eq("deleted", false).exe();

		if (exe.size() != 0) {
			return Result.error(-1, "该文件夹已存在！");
		}
    	
    	jdbcSession.insert(question);
		if (Root.isRoot(question.getPid())) {
			return Result.success(question);
		}
		return Result.success();
    }

	@Bean(copy=true)
	@HasPermission("crm:question:edit")
    @ReqMapping("/update")
    public Object update(QuestionPlatform question, ValidResult result) {
    	if (result.hasError("isFolder")) {
    		return Result.error(0, result.getErrorMessage());
    	}
		QuestionPlatform resultQuestion = Pool.get(QuestionPlatform.class, question.getId());
		if (Objects.isNull(resultQuestion)) {
			return Result.error(-1, "该文件夹不存在！");
		}

		question.setFolder(resultQuestion.isFolder());
    	
    	jdbcSession.updateById(question);
    	return Result.success();
    }

	@HasPermission("crm:question:edit")
    @ReqMapping("/delete")
    public Object delete(String id) throws Exception {
		QuestionPlatform question = Pool.get(QuestionPlatform.class, id);
		if (Root.isRoot(question.getPid())) {

			jdbcSession.deleteById(question);
			List<QuestionPlatform> list = jdbcSession.findArray(QuestionPlatform.class)
					.eq("pid", question.getId())
					.eq("deleted", false)
					.exe();
			jdbcSession.deleteById(list);
		} else {
			jdbcSession.deleteById(question);
		}
    	return Result.success();
    }

}
